package com.googlecode.shapeline.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import net.sf.json.JSON;
import net.sf.json.JSONObject;
//import net.sf.json.xml.XMLSerializer;

//import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
//import org.apache.struts.action.ActionMessage;
//import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

import com.googlecode.shapeline.common.Consts;
import com.googlecode.shapeline.core.User;
import com.googlecode.shapeline.service.UserService;
import com.googlecode.shapeline.util.Message;

public class LoginAction extends DispatchAction {
	private static final org.apache.log4j.Logger log = org.apache.log4j.LogManager.getLogger(LoginAction.class);
	private UserService userService;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	//@SuppressWarnings("unchecked")
	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request,
			HttpServletResponse response)
			throws IOException, ServletException {
		
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		log.debug("login : " + name + ", " + password);
		
		Message message = new Message();
		message.setAction("login");
		try {
			User user = getUserService().login(name, password);
			request.getSession(true).setAttribute(Consts.SESSION_KEY_LOGIN, user);
			
			message.setMessage("");
			message.setObject(user);
			message.setSuccess(true);
		}
		catch (Exception e) {
			message.setMessage(e.getMessage());
			message.setObject(null);
			message.setSuccess(false);
		}
		
		JSONObject jsonObject = JSONObject.fromObject(message);
		request.setAttribute("json", jsonObject);
		log.debug(jsonObject);
		
		return mapping.findForward("json");
		
		/*
		try {
			User user = new User();
			user.setId(++i);
			user.setName(name);
			user.setPassword(password);
			user.setProtected(false);
			user.setRoles(null);
			//json debug
			try {
			//JSONObject jsonObject = JSONObject.fromObject("{\"id\":1,\"name\":\"user1\",\"password\":\"123456\",\"protected\":false,\"roles\":[\"admin\",\"assistant\"]}");
			JSONObject jsonObject = JSONObject.fromObject(user);
			log.debug(jsonObject);
			Class clazz = Class.forName("com.googlecode.shapeline.core.User");
			Object obj = JSONObject.toBean(jsonObject, clazz); 
			log.debug(((User)obj).getName());
			XMLSerializer xmlSerializer = new XMLSerializer();
			xmlSerializer.setTypeHintsEnabled( false );
			String xml = xmlSerializer.write(jsonObject);
			log.debug(xml);
			
			XMLSerializer xmlSerializer2 = new XMLSerializer();
			JSON json = xmlSerializer2.read( xml );
			log.debug(json.toString(2));
			}
			catch (Exception e) {
				log.error("error when try json", e);
			}
		}
		catch (Exception e) {
		}
		*/
		
	}
}
